AttributeSetting=function (editor) {
	this.editor = editor;
	this.setting = this.getCurrentAttributeSetting();
	this.validator = new AttributeValidation();
};

AttributeSetting.prototype.setting = null;
AttributeSetting.prototype.editor = null;

AttributeSetting.prototype.parentDialogId=null;
AttributeSetting.prototype.validator=null;

AttributeSetting.prototype.getParentDialogId = function() {
	return this.setting.tagName.toLowerCase();
};

AttributeSetting.prototype.getCurrentWorkflow = function () {
    var workflow = this.editor.graph.getWorkflow();
	//var name = workflow.getAttribute('Name');
	//var version = workflow.getAttribute('Version');
    return workflow;
}

AttributeSetting.prototype.getUrl = function () {
	var url = location.href;
	var lastIndex = url.lastIndexOf("/");
	return url.substring(0, lastIndex).replace('WorkflowDesigner', '');
};

AttributeSetting.prototype.getCurrentAttributeSetting=function (type) {
	var graph = this.editor.graph;
	var model = graph.getModel();
    var cell =null;
	if("process"==type){
        cell = graph.getModel().getRoot();
	}else {
		cell = graph.getSelectionCell();
	}
	if (cell == null) {
		cell = graph.getCurrentRoot();
		if (cell == null) {
			cell = graph.getModel().getRoot();
		}
	}
	var setting=cell.value;
	if($(setting).find('Extras')[0]==null){
		var doc = this.createXmlDocument();
		var extras = doc.createElement('Extras');
		setting.appendChild(extras);
	}	
	return setting;
};

AttributeSetting.prototype.getCurrentCell=function () {
	var graph = this.editor.graph;
	var cell = graph.getSelectionCell();
	return cell;
}
	
AttributeSetting.prototype.updateAttributeSetting=function(){   	
	var graph = this.editor.graph;
	var model = graph.getModel();
	var cell = graph.getSelectionCell();
	model.beginUpdate();
	try {
		model.setValue(cell, this.setting);
	} finally {
		model.endUpdate();
	}
};
AttributeSetting.prototype.createXmlDocument =function() {
		var doc = null;
		document.implementation && document.implementation.createDocument
				? doc = document.implementation.createDocument("", "", null)
				: window.ActiveXObject
						&& (doc = new ActiveXObject("Microsoft.XMLDOM"));
		return doc;
};

AttributeSetting.prototype.popTipsDialog = function(dialogInfo) {
        parent.$('body').append($('<div title="提示信息" style="display:none"/>')
				.attr('id', 'dialog').append($('<p>'+dialogInfo+'</p>')));
		parent.$("#dialog").dialog({
					autoOpen : true,
					modal : true,
					zIndex : 9999,
					resizable : false,
					close : function(event, ui) {
						$(event.target).dialog("destroy");
					}
				});
};
AttributeSetting.prototype.inputValidate = function() {
	return efvalidateDiv("basicAttrInputs");
};
AttributeSetting.prototype.validateProcessesIsExist=function(processNameArr) {	
	var message="";
	var info = new EiInfo();	
	info.set('processNames', processNameArr);
	EiCommunicator.send('EWPE00', 'verifyProcessRef', info, {
		onSuccess : function(outInfo) {
			var result = outInfo.get('processNames');
			if (result.length > 0)
				message = "相关流程未定义：" + result;
		},
		onFail : function(err) {
			message = err;
		}
	}, false, false);
	
	return message;
}

AttributeSetting.prototype.removeSelectedRow = function(grid_id) 
{
	
	var grid = efform.getGrid( grid_id );
	var count = grid.getCheckedRowCount();
			if(count<=0)
	        {
		     alert("请先选择再进行删除操作!","警告");
		     return;
	        }
	var checkedRows=grid.getCheckedRows();
	for(var i=0;i<count;i++)
	{
		 grid.removeRow(checkedRows[i]-i);
	}
	
	 grid.refresh();
	 
	 for( i=0;i<grid._rowStyle.length;i++)
	 {
		 grid._rowStyle[i]="tableRow"+i%2;
	 }
};

AttributeSetting.prototype.newRecord = function(grid_id) {
	var grid = efform.getGrid(grid_id);
	if (grid) {
		var copy = false;
		if (typeof(efgrid_onAddNewRow) == "function") {
			// 调用用户自定义函数判断是否允许新增
			try {
				if (efgrid_onAddNewRow(grid_id) === false)
					return;
			} catch (ex) {
			}
		}
		var selcount = grid.getCheckedRowCount();
		if (selcount > 0)
			copy = confirm("[" + selcount + "]"
					+ getI18nMessages("ef.GridRowsCopy", "条记录被选中，将它们复制为新记录吗？"));

		grid.newLine(copy);

		if (typeof(efgrid_afterAddNewRow) == "function") {
			// 定义回调 新增数据之后的回调函数
			try {
				efgrid_afterAddNewRow(grid_id)
			} catch (ex) {
			}
		}

		grid.refresh();

		var grid_table = document.getElementById(grid_id + EF_SPLIT
				+ "grid_table");
		var dataDiv = efgrid.getDataDiv(grid_table);
		dataDiv.scrollTop = dataDiv.scrollHeight;

		var rows = grid.getCheckedRows();
		for (var i = 0; i < rows.length; i++) {
			if (grid.isNewLine(rows[i])) {
				grid.cellOriginNodeType = TYPE_FIX;
				efgrid._shiftCell(grid, rows[i], 0, 2);
				break;
			}
		}
	}
};

AttributeSetting.prototype.validateExtra= function(){
    var grid = $("#ef_grid_extraResult").data("kendoGrid");//整个表格
    var datas = grid.getCheckedRows();//所有选中行
	var nameArr = [];
	var isValidate=true,errMsg="";
	if (datas.length > 0){
	   var name,value;
		for (var i = 0; i < datas.length; i++) {
			name=datas[i].Name.trim();
			value=datas[i].Value.trim();
			
			if(name==""||value=="")
		    errMsg="扩展属性名称或值不能为空!\n";
		    if(name!="")
			nameArr.push(name);
		}
		var dupMsg=this.validateDuplicateName(nameArr);
		if(dupMsg!="")errMsg+="存在重复的扩展属性名称:"+dupMsg+"!";	
		
	}
	return errMsg;
};


AttributeSetting.prototype.validateDuplicateName=function(nameArr){
var validateMsg="";
var duplicateNames = new Array();
	for (var i = 0; i < nameArr.length; i++) {
		var curEname = nameArr[i];
		var count = 0;
		for (var j = 0; j < nameArr.length; j++) {
			if (nameArr[j] == curEname)
				count++;
		}
		if (count > 1) {
			var isExist = false;
			for (var j = 0; j < duplicateNames.length; j++) {
				if (duplicateNames[j] == curEname)
					isExist = true;
			}
			if (!isExist)
				duplicateNames.push(curEname);
		}
	}
	
	if (duplicateNames.length != 0) {
		validateMsg+='' + duplicateNames+'\n';
	}
	return validateMsg;
	
};